<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1200px">
			<el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" size="large" label-width="80px">
				 <el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8" class="mb20">
						<el-form-item label="关联类型" prop="linkType">
							<el-radio-group v-model="state.ruleForm.linkType" @change="onChangeLink()">
                        		<el-radio-button :label="0">企业</el-radio-button>
                        		<el-radio-button :label="1">个人</el-radio-button>
                        	</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="8" class="mb20" id="company" v-if="state.ruleForm.linkType == 1" style="display:none">
						<el-form-item label="企业" prop="linkId">
							<el-select
								v-model="state.ruleForm.companyId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联企业"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.companyListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="company" v-if="state.ruleForm.linkType == 0">
						<el-form-item label="企业" prop="linkId">
							<el-select
								v-model="state.ruleForm.companyId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联企业"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.companyListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="person"  v-if="state.ruleForm.linkType == 0" style="display:none">
						<el-form-item label="自然人" prop="linkId">
							<el-select
								v-model="state.ruleForm.personId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联自然人"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.personListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="person"  v-if="state.ruleForm.linkType == 1">
						<el-form-item label="自然人" prop="linkId">
							<el-select
								v-model="state.ruleForm.personId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联自然人"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.personListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="起拍价" prop="startingPrice">
							<el-input v-model="state.ruleForm.startingPrice" placeholder="请输入起拍价" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="成交价" prop="endPrice">
							<el-input v-model="state.ruleForm.endPrice" placeholder="请输入成交价" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8" class="mb20" >
						<el-form-item label="开始时间" prop="startTime">
							<el-date-picker v-model="state.ruleForm.startTime"	type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请输入开始时间" size="large" />
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8" class="mb20" >
						<el-form-item label="结束时间" prop="endTime">
							<el-date-picker v-model="state.ruleForm.endTime"	type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请输入结束时间" size="large" />
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="16" class="mb20">
						<el-form-item label="状态" prop="isPass">
							<el-radio-group v-model="state.ruleForm.isPass">
                        		<el-radio-button :label="0">未开始</el-radio-button>
                        		<el-radio-button :label="1">进行中</el-radio-button>
                        		<el-radio-button :label="2">已成交</el-radio-button>
                        		<el-radio-button :label="3">流拍</el-radio-button>
                        	</el-radio-group>
						</el-form-item>
					</el-col>
                </el-row>

			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="cgauctionForm">
import { reactive, defineAsyncComponent,onMounted, ref ,unref,nextTick} from 'vue';
import { getinfo,save } from '/@/api/cgauction/index';
import { getListAll as getPersonList } from '/@/api/cgperson/index';
import { getListAll as getCompanyList } from '/@/api/cgcompany/index';
import {  ElMessage } from 'element-plus';


// 引入组件
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
const Uploadimg = defineAsyncComponent(() => import('/@/components/Upload/singleImage.vue'));
const Uploadvedio = defineAsyncComponent(() => import('/@/components/Upload/vedio.vue'));

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


// 定义变量内容
const activeName = ref('first');
const dialogFormRef = ref();
const state = reactive({
	ruleForm: {
    name:'',
    linkType:'',
    linkId:'',
    startTime:'',
    endTime:'',
    startingPrice:'',
    endPrice:'',
    isPass:'',
    id:'',
  },
	
  	companyListData:[],
	personListData:[],	
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	// 表单校验
	ruleRules:  { 
    name: [{ required: true, message: 'name不能为空', trigger: 'blur' }],
  },
	loading: false,
	msg:'最佳尺寸100*100',
});


const onChangeLink = () => {
	if(state.ruleForm.linkType == 0) {
		document.getElementById("company").style.display = "";
		document.getElementById("person").style.display = "none";
	} else {
		document.getElementById("company").style.display = "none";
		document.getElementById("person").style.display = "";
	}
};
const onUpload = (data) => {
	state.ruleForm.img = data;
};

const onUploadvedio = (data) => {
	state.ruleForm.video = data;
};

// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		// 实际请走接口
		getinfo(row.id).then((response: any) => {
			state.ruleForm.name = response.data.name;
			state.ruleForm.linkType = response.data.linkType;
			if(state.ruleForm.linkType == 0) {
				state.ruleForm.companyId = response.data.linkId;
			} else {
				state.ruleForm.personId = response.data.linkId;
			}
			state.ruleForm.startTime = response.data.startTime;
			state.ruleForm.endTime = response.data.endTime;
			state.ruleForm.startingPrice = response.data.startingPrice;
			state.ruleForm.endPrice = response.data.endPrice;
			state.ruleForm.isPass = response.data.isPass;
			state.ruleForm.id = response.data.id;

		});
		
		state.dialog.title = '修改';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dialogFormRef.value.resetFields();
			initForm();
		});
	}
	getCompanyList({fileds:['id','name']}).then((response: any) => {
		state.companyListData = response.data.data
	});
	getPersonList({fileds:['id','name']}).then((response: any) => {
		state.personListData = response.data.data
	});
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	// 查询
	
};
// 关闭弹窗
const closeDialog = () => {
	initForm();
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {

	const formWrap = unref(dialogFormRef) as any;
	if (!formWrap) return;
	formWrap.validate((valid: boolean) => {
		if (valid) {
			if(state.ruleForm.linkType == 0) {
				state.ruleForm.linkId = state.ruleForm.companyId;
			} else {
				state.ruleForm.linkId = state.ruleForm.personId;
			}
			if(state.ruleForm.startTime == null || state.ruleForm.startTime == '') {
				state.ruleForm.startTime = "1970-01-01 00:00:00";
			}
			if(state.ruleForm.endTime == null || state.ruleForm.endTime == '') {
				state.ruleForm.endTime = "1970-01-01 00:00:00";
			}
			state.loading = true;
			save(state.ruleForm)
				.then(() => {
					ElMessage.success('保存成功');
					state.loading = false;
					closeDialog(); // 关闭弹窗
					emit('refresh');
				})
				.finally(() => {
					state.loading = false;
				});
		}
	})
};
// 页面加载时
onMounted(() => {
	
});


// 表单初始化，方法：`resetFields()` 无法使用
const initForm = () => {
	state.ruleForm.name = '';
	state.ruleForm.linkType = '';
	state.ruleForm.companyId = "";
	state.ruleForm.personId = "";
	state.ruleForm.linkId = '';
	state.ruleForm.startTime = '';
	state.ruleForm.endTime = '';
	state.ruleForm.startingPrice = '';
	state.ruleForm.endPrice = '';
	state.ruleForm.isPass = '';
	state.ruleForm.id = '';

};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
